package no.saiboten.tourbuilder.tournament.controller.admin;

import java.text.MessageFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import no.saiboten.tourbuilder.tournament.TournamentSize;
import no.saiboten.tourbuilder.tournament.service.TournamentService;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

@Controller
@RequestMapping("/tournament/admin")
public class TournamentAdminController {

	final Logger LOGGER = Logger.getLogger(getClass());

	@Autowired
	TournamentService tournamentService;

	@RequestMapping
	public ModelAndView viewTournaments() {
		ModelAndView mav = new ModelAndView("tournament/tournament_admin");
		mav.addObject("tournaments", tournamentService.findAllTournaments());
		return mav;
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public ModelAndView addTournament(@RequestParam String name,
			@RequestParam String startDate, @RequestParam String endDate,
			@RequestParam TournamentSize size) {

		Date start = parseDate(startDate);
		Date end = parseDate(endDate);

		if (start != null && end != null) {
			tournamentService.createTournament(name, start, end, size);
			RedirectView redirectView = new RedirectView(
					"/tourbuilder/tournament/get/" + name);
			return new ModelAndView(redirectView);
		} else {
			LOGGER.debug("Some dates could not be parsed: startdate: "
					+ startDate + " - enddate: " + endDate);
		}

		LOGGER.debug(MessageFormat.format(
				"Name {0}, startdate: {1}, endDate, {2}, size: {3}", name,
				startDate, endDate, size));

		ModelAndView mav = new ModelAndView("tournament/tournament_admin");
		mav.addObject("result", "Something went wrong ..");
		return mav;
	}

	@RequestMapping("/delete/{tournamentId}")
	public ModelAndView deleteTournament(@PathVariable String tournamentId) {
		LOGGER.debug("Deleting tournament: " + tournamentId);
		ModelAndView mav = new ModelAndView("tournament/tournament_admin");

		boolean deleted = tournamentService.deleteTournament(tournamentId);
		LOGGER.debug("Was it deleted? " + deleted);

		if (deleted) {
			mav.addObject("result", "Deleted tournament with id: "
					+ tournamentId);
		} else {
			mav.addObject("result",
					"Could not delete tournament. Does it exist?");
		}
		return mav;
	}

	@RequestMapping("/removeUser/{user}")
	public ModelAndView removeUserFromTournament(@PathVariable String user) {
		ModelAndView mav = new ModelAndView("tournament/tournament_admin");

		return mav;
	}

	private Date parseDate(String startDate) {
		// 19.08 19:30
		SimpleDateFormat sdf = new SimpleDateFormat("MM.dd k:m");
		Date date = null;
		try {
			date = sdf.parse(startDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}

}
